﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualTournaments.DataAccess;
using VirtualTournaments.DataAccess.EntityManagers;

namespace VirtualTournaments.Services.Tournaments
{
    public class TeamsService
    {
        public static Team[] GetTeamsByTournamentName(string tournamentName)
        {
            List<Team> result = new List<Team>();
            ITeamsManager teamsManager = DataAccessManager.EntityManagersFactory.GetTeamsManager();
            
            ITournamentsManager tournamentsManager = DataAccessManager.EntityManagersFactory.GetTournamentsManager();
            
            tournamentsManager.GetBySimilarName(tournamentName).ToList().ForEach(businessTournament => 
             teamsManager.GetByTournament(businessTournament).ToList().ForEach(team => result.Add(Bussiness2ServiceConversor.ConvertTeam(team)))
            );

            return result.ToArray();
        }

        public static Team[] GetAllTeams()
        {
            /*List<Team> result = new List<Team>();

            ITournamentsManager tournamentsManager = DataAccessManager.EntityManagersFactory.GetTournamentsManager();
            List<Business.Tournaments.Tournament> businessTournaments = tournamentsManager.GetAll().ToList();
            foreach (var bizTournament in businessTournaments)
            {
                foreach (var team in bizTournament.Teams)
                {
                    result.Add(Bussiness2ServiceConversor.ConvertTeam(team));
            
                }
            }

            return result.ToArray();
             */
            List<Team> result = new List<Team>();
            ITeamsManager teamsManager = DataAccessManager.EntityManagersFactory.GetTeamsManager();

            ITournamentsManager tournamentsManager = DataAccessManager.EntityManagersFactory.GetTournamentsManager();

            tournamentsManager.GetBySimilarName("Torneo Clausura 2009").ToList().ForEach(businessTournament =>
             teamsManager.GetByTournament(businessTournament).ToList().ForEach(team => result.Add(Bussiness2ServiceConversor.ConvertTeam(team)))
            );

            return result.ToArray();
        }
    }
}
